import { IOption, IFormItem, FormItemType } from '@shared/interfaces/common'
import { numScale, compareTwoCurrencies } from 'app/utils/helpers'
import { WXChannel } from '@shared/interfaces/acquirer'
import { Validators } from '@angular/forms'
import {
  WeChatMerchantIDMaxLength,
  WeChatMerchantIDReg,
  TransactionFeeReg,
  ChannelMerchantNameMaxLength,
  ApiSecretMaxLength,
  ApiSecretMinLength,
  APiSecretReg,
  WeChatAppIDMaxLength,
  WeChatAppIDReg,
} from 'app/utils/contraints'
import { Injectable } from '@angular/core'
import { I18NService } from '@core'

@Injectable()
export class WXPService {
  constructor(private i18n: I18NService) {}
  genWXFormItems = (currencyOptions: IOption[], handleChanMerIDChange): IFormItem[] => {
    return [
      {
        label: 'WeChat Merchant ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'WeChat Merchant ID' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'WeChat Merchant ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: WeChatMerchantIDMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        },
        formControlName: 'chanMerID',
        change: handleChanMerIDChange,
      },
      {
        label: 'API Certificate(CA issued)',
        errTip: '',
        type: FormItemType.FILE,
        formControlName: 'publicKey',
      },
      {
        label: 'API Certificate private key',
        errTip: '',
        type: FormItemType.FILE,
        formControlName: 'privateKeyArray',
      },
      {
        label: 'WeChat APP ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'WeChat APP ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: WeChatAppIDMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        },
        formControlName: 'appID',
      },
      {
        label: 'API Secret',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'API Secret' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ApiSecretMaxLength }),
          minLength: this.trans('VALIDATION.MIN_LENGTH', { length: ApiSecretMinLength }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        },
        errTip: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        formControlName: 'signKey',
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        compareFn: compareTwoCurrencies,
        options: currencyOptions,
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelMerchantNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genTRUalidateRules = (channel: WXChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
    }
  }

  genWXValidateRules = (channel: WXChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      chanMerID: [
        channel && channel.chanMerID,
        [Validators.required, Validators.maxLength(WeChatMerchantIDMaxLength), Validators.pattern(WeChatMerchantIDReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.min(0), Validators.max(10), Validators.pattern(TransactionFeeReg)],
      ],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      signKey: [
        channel && channel.signKey,
        [
          Validators.required,
          Validators.maxLength(ApiSecretMaxLength),
          Validators.minLength(ApiSecretMinLength),
          Validators.pattern(APiSecretReg),
        ],
      ],
      publicKey: [channel && channel.publicKey, [Validators.required]],
      privateKeyArray: [channel && channel.privateKeyArray, [Validators.required]],
      appID: [
        channel && channel.appID,
        [Validators.required, Validators.maxLength(WeChatAppIDMaxLength), Validators.pattern(WeChatAppIDReg)],
      ],
      isLoaded: [channel && channel.isLoaded],
    }
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
